// pages/deviceList/deviceList.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [],
    showDialog: false,
    newSeri: '',
    newPhone: '',
    newName: '',
    newIdCard: '',
    searchContent: '',
    page: 1,
    pageSize: 10,
    isLoading: false,
    noMoreData: false,
    isRefreshing: false,
    showDialog: false,
    scrollyHeight: wx.getSystemInfoSync().windowHeight,
    accountType: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      statusBarHeight: getApp().px2rpx(wx.getSystemInfoSync().statusBarHeight + 44),
      scrollyHeight: getApp().px2rpx(wx.getSystemInfoSync().windowHeight - (wx.getSystemInfoSync().statusBarHeight + 44)) - 126,
      accountType: getApp().globalData.accountType
    })
  },

  goDeviceDetail: function (e) {
    let id = e.currentTarget.dataset.id
    let state = e.currentTarget.dataset.state
    wx.navigateTo({
      url: '/pages/deviceDetail/deviceDetail?id=' + id + '&state=' + state,
    })
  },
  goDetail: function (e) {
    let id = e.currentTarget.dataset.id
    let state = e.currentTarget.dataset.state
    if (this.data.accountType == 6) {
      return
    }
    wx.navigateTo({
      url: '/pages/deviceDetail/deviceDetail?id=' + id + '&state=' + state,
    })
  },


  search: function (e) {
    let that = this
    let searchContent = e.detail.value
    that.setData({
      searchContent,
      page: 1
    })
    setTimeout(() => {
      that.setData({
        isRefreshing: true,
        noMoreData: false
      });
      let accountType = this.data.accountType
      if (accountType == 2 || accountType == 3 || accountType == 5) {
        that.deviceQueryPage(true)
      } else {
        if ((accountType == 4 || accountType == 6) && getApp().globalData.MerchantStoreInfo) {
          that.deviceQueryPage(true)
        }
        that.queryPageMyDevice(true);
      }
    }, 100)

  },

  /**
   * 获取监管锁设备列表
   */
  deviceQueryPage: function (refresh = false) {
    var that = this;
    if (this.data.isLoading) return;

    this.setData({
      isLoading: true
    });
    const {
      page,
      pageSize
    } = this.data;
    wx.showLoading({
      title: '加载中',
    })
    let params = {
      page: page,
      limit: pageSize,
      deviceStatusList: [0, 1, 2, 3, 4]
    }
    if (getApp().globalData.MerchantStoreInfo && getApp().globalData.MerchantStoreInfo.merchantId) {
      params.storeMerchantId = getApp().globalData.MerchantStoreInfo.merchantId
    }
    if (this.data.searchContent) {
      params.searchContent = this.data.searchContent
    }
    getApp().api.deviceQueryPage(params, {
      success: function (res) {
        if (res.data.rows) {
          let items = res.data.rows
          items = refresh ? items : that.data.items.concat(items),
            that.setData({
              items: items,
              noMoreData: res.data.total > items.length ? false : true,
              isRefreshing: false,
              isLoading: false
            })
          wx.hideLoading()
        }
      },
      fail: function (err) {
        that.setData({
          isLoading: false
        });
        wx.showToast({
          title: err.message,
          icon: 'none',
          success: function () {
            if (refresh) {
              that.setData({
                items: null
              })
            }
          }
        })
        wx.hideLoading()
      }
    });
  },

  inputPhone: function (e) {
    console.log(e)
    let that = this
    let searchContent = e.detail.value
    that.setData({
      searchContent,
      page: 1
    })
    setTimeout(() => {
      that.setData({
        isRefreshing: true,
        noMoreData: false
      });
      let accountType = this.data.accountType
      if (accountType == 2 || accountType == 3 || accountType == 5) {
        that.deviceQueryPage(true)
      } else {
        if ((accountType == 4 || accountType == 6) && getApp().globalData.MerchantStoreInfo) {
          that.deviceQueryPage(true)
        }
        that.queryPageMyDevice(true);
      }
    }, 2000)


  },
  /**
   * 获取设备列表
   */
  queryPageMyDevice: function (refresh = false) {
    var that = this;
    if (this.data.isLoading) return;

    this.setData({
      isLoading: true
    });
    const {
      page,
      pageSize
    } = this.data;
    wx.showLoading({
      title: '加载中',
    })
    let params = {
      page: page,
      limit: pageSize,
      deviceStatusList: [0, 1, 2, 3, 4]
    }
    if (this.data.searchContent) {
      params.searchContent = this.data.searchContent
    }
    if (getApp().globalData.MerchantStoreInfo && getApp().globalData.MerchantStoreInfo.merchantId) {
      params.storeMerchantId = getApp().globalData.MerchantStoreInfo.merchantId
    }
    getApp().api.queryPageMyDevice(params, {
      success: function (res) {
        if (res.data.rows) {
          let items = res.data.rows
          items = refresh ? items : that.data.items.concat(items),
            that.setData({
              items: items,
              noMoreData: res.data.total > items.length ? false : true,
              isRefreshing: false,
              isLoading: false
            })
          wx.hideLoading()
        }
      },
      fail: function (err) {
        that.setData({
          isLoading: false
        });
        wx.showToast({
          title: err.message,
          icon: 'none',
          success: function () {
            if (refresh) {
              that.setData({
                items: null,
              })
            }
          }
        })

        wx.hideLoading()
      }
    });
  },

  loadMore() {
    if (this.data.noMoreData || this.data.isRefreshing) return;
    this.setData({
      page: this.data.page + 1
    })
    let accountType = this.data.accountType
    if (accountType == 2 || accountType == 3 || accountType == 5) {
      this.deviceQueryPage()
    } else {
      if ((accountType == 4 || accountType == 6) && getApp().globalData.MerchantStoreInfo) {
        this.deviceQueryPage()
      } else {
        this.queryPageMyDevice();
      }
    }
  },

  refresh() {
    this.setData({
      isRefreshing: true,
      noMoreData: false,
      page: 1
    });

    let accountType = this.data.accountType
    if (accountType == 2 || accountType == 3 || accountType == 5) {
      this.deviceQueryPage(true)
    } else {
      if ((accountType == 4 || accountType == 6) && getApp().globalData.MerchantStoreInfo) {
        this.deviceQueryPage(true)
      } else {
        this.queryPageMyDevice(true);
      }
    }
  },




  showDialog: function () {
    this.setData({
      showDialog: true
    })
  },


  inputPopInfo: function (e) {
    let type = e.currentTarget.dataset.type
    let value = e.detail.value
    switch (type) {
      case "1":
        this.setData({
          newSeri: value
        })
        break;
      case "2":
        this.setData({
          newName: value
        })
        break;
      case "3":
        this.setData({
          newPhone: value
        })
        break;
      case "4":
        this.setData({
          newIdCard: value
        })
        break;
    }
  },

  cancle: function () {
    this.setData({
      showDialog: false
    })
  },

  confirmDialog: function () {
    //TODO 去执行添加
    if (this.data.newSeri == '') {
      wx.showToast({
        title: '请输入序列号',
        icon: 'none'
      })
      return
    }
    if (this.data.newIdCard != '') {
      const idRegExp = /^\d{6}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])\d{3}(\d|X|x)$/;
      if (!idRegExp.test(this.data.newIdCard)) {
        wx.showToast({
          title: '请输入正确的身份证号',
          icon: 'none'
        })
        return
      }
    }
    this.storeAddDeviceByWaitAssign()
  },


  /**
   * 添加设备号
   */
  storeAddDeviceByWaitAssign: function () {
    var that = this;
    const {
      newSeri,
      newName,
      newPhone,
      newIdCard
    } = this.data;
    let params = {
      serialNumber: newSeri,
      storeMerchantId: getApp().globalData.MerchantStoreInfo.merchantId || ''
    }
    if (newName) {
      params.possessor = newName
    }
    if (newPhone) {
      params.mobilePhone = newPhone
    }
    if (newIdCard) {
      params.possessorCard = newIdCard
    }
    getApp().api.storeAddDeviceByWaitAssign(params, {
      success: function (res) {
        wx.showToast({
          title: '添加设备成功',
          duration: 3000,
          icon: 'none'
        })
        that.setData({
          showDialog: false,
          newSeri: '',
          newName: '',
          newPhone: '',
          newIdCard: ''
        })
        let accountType = that.data.accountType
        if (accountType == 2 || accountType == 3 || accountType == 5) {
          that.deviceQueryPage(true)
        } else {
          if ((accountType == 4 || accountType == 6) && getApp().globalData.MerchantStoreInfo) {
            that.deviceQueryPage(true)
          } else {
            that.queryPageMyDevice(true);
          }

        }
        wx.hideLoading()
      },
      fail: function (err) {
        wx.showToast({
          title: '添加设备失败' + err.message,
          duration: 3000,
          icon: 'none'
        })
        wx.hideLoading()
      }
    });
  },


  /**
   * 同步设备
   */
  syncDevices: function () {
    var that = this;
    let params = {}
    wx.showLoading({
      title: '正在同步设备',
    })
    getApp().api.deviceSync(params, {
      success: function (res) {
        wx.showToast({
          title: '同步设备成功',
          duration: 3000,
          icon: 'none'
        })

        let accountType = that.data.accountType
        if (accountType == 2 || accountType == 3 || accountType == 5) {
          that.deviceQueryPage(true)
        } else {
          if ((accountType == 4 || accountType == 6) && getApp().globalData.MerchantStoreInfo) {
            that.deviceQueryPage(true)
          } else {
            that.queryPageMyDevice(true);
          }
        }
        wx.hideLoading()
      },
      fail: function (err) {
        wx.showToast({
          title: '同步设备失败' + err.message,
          duration: 3000,
          icon: 'none'
        })
        wx.hideLoading()
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let accountType = getApp().globalData.accountType
    this.setData({
      accountType,
      page: 1
    })
    if (accountType == 2 || accountType == 3 || accountType == 5) {
      this.deviceQueryPage(true)
    } else {
      if ((accountType == 4 || accountType == 6) && getApp().globalData.MerchantStoreInfo) {
        this.deviceQueryPage(true)
      } else {
        this.queryPageMyDevice(true);
      }

    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})